//
//  VisitorLoginView.swift
//  0110-weibo
//
//  Created by 鲍志刚 on 16/1/10.
//  Copyright © 2016年 zhongye. All rights reserved.
//

import UIKit

//导入SnapKit命名空间

import SnapKit


//1.protocol:swift 中 没有 ‘@’
//2.遵守跟协议 <NSObject>
//3.如果不遵守 NSObjectProtocol<基协议> 就是无法使用 weak 关键字
//4.声明可选的议席方法 需要使用 optional 关键字 是oc中的关键字 需要在声明的协议前
@objc protocol VisitorLoginViewDelegate : NSObjectProtocol {

    //协议方法
//  //用户将要注册
    func userWillRegister()
    
    //用户将要登录
    func userWillLogin()
    
}

class VisitorLoginView: UIView {
    
    //声明代理 swift属性 默认的属性关键字 就是 strong
    //声明弱引用的属性 需要 weak
    weak var visitorLoginViewDelegate:VisitorLoginViewDelegate?
    
    
    //MARK:按钮的监听事件方法
    @objc private func loginBtnDidClick(){
        //使用代理调用协议方法
        
        //if (代理 ！＝ nil)
        visitorLoginViewDelegate?.userWillLogin()
    }
    
    
    @objc private func registerBtnDidClick(){
        
        visitorLoginViewDelegate?.userWillRegister()
        
    }
    
    
    
    //外界调用的重要方法
    func setupUIWithInfo(imageName:String?, tipText:String) {
        if let img = imageName {
        circleIcon.image = UIImage(named: img)
            
            //隐藏大房子
            largeHouse.hidden = true
            
        bringSubviewToFront(circleIcon)
        
        
        } else {
        strartAnimation()
        
        }
    tipLabel.text = tipText
    }
    
    
    //动画效果
    func strartAnimation(){
    let base = CABasicAnimation(keyPath: "transform.rotation")
        
        //设置重复次数
        base.repeatCount = MAXFLOAT
        base.toValue = 2 * M_PI
        
        
        base.duration = 10
        
        //设置动画完成 页面失去活跃状态 不移除涂层
        base.removedOnCompletion = false
        
        circleIcon.layer.addAnimation(base, forKey: nil)
    
    
    }
    
    
    
    
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        //添加子试图
        addSubview(circleIcon)
 
        addSubview(backView)
        
        addSubview(largeHouse)
        
        addSubview(tipLabel)
        
        addSubview(loginBtn)
        
        addSubview(registerBtn)
        
        
        //设置自动布局
        /*
        添加约束 推荐 使用链式约束
        */
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            //垂直居中 添加偏移量
            make.centerY.equalTo(self.snp_centerY).offset(-60)
        }
        
        largeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(circleIcon)
            
            make.top.equalTo(circleIcon.snp_bottom).offset(16)
            
            make.width.equalTo(225)
            
            make.height.equalTo(40)
          
        }
        
        //登录按钮
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            
            make.right.equalTo(tipLabel.snp_right)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.width.equalTo(100)
        }
        
        backView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            
            make.bottom.equalTo(registerBtn.snp_bottom)
        }
        
        
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        
    }
    
    
    
    
    //懒加载获取所有的子试图
    //背景
    lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    lazy var backView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))

    lazy var largeHouse : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    
    //提示文案
    lazy var tipLabel :UILabel = UILabel(title: "登录后，就可以评论别人的微博，并且自己的微博别人也可以看得到", fontSize: 14, color: UIColor.darkGrayColor())
    
//    {
//    
//        let l = UILabel()
//        l.font = UIFont.systemFontOfSize(14)
//        l.text = "登录后，就可以评论别人的微博，并且自己的微博别人也可以看得到"
//        l.numberOfLines = 0
//        l.textColor = UIColor.darkGrayColor()
//        l.textAlignment = .Center
//    
//        
//        l.sizeToFit()
//        
//        return l
//    
//    
//    }()
    
    //登录按钮
    lazy var loginBtn : UIButton = UIButton(title: "登录", fontSize: 16, color: UIColor.darkGrayColor(), backImageName: "common_button_white_disable")
    
//    {
//    let btn = UIButton()
//        //设置按钮文字
//        btn.setTitle("登录", forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
//        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: .Normal)
//    
//    return btn
//    
//    }()
    
    
    //注册按钮
    lazy var registerBtn:UIButton = UIButton(title: "注册", fontSize: 16, color: UIColor.orangeColor(), backImageName: "common_button_white_disable")
    
//    {
//       
//        let btn = UIButton()
//        //设置按钮的文字
//        btn.setTitle("注册", forState: .Normal)
//        btn.titleLabel?.font = UIFont.systemFontOfSize(18)
//        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
//        
//        let image = UIImage(named: "common_button_white_disable")!
//        let w = Int(image.size.width * 0.5)
//        let h = Int(image.size.height * 0.5)
//        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
//        
//        return btn
//        
//        
//    }()
    
    

}
